import sqlite3
from datetime import datetime, timedelta

# 示例数据
SAMPLE_DATA = [
    {'date': '2024-01-01', 'cny': 10000.0, 'usd': 1389.0, 'exchange_rate': 7.2},
    {'date': '2024-01-15', 'cny': 15000.0, 'usd': 2083.0, 'exchange_rate': 7.2},
    {'date': '2024-02-01', 'cny': 8000.0, 'usd': 1111.0, 'exchange_rate': 7.2},
    {'date': '2024-02-15', 'cny': 12000.0, 'usd': 1667.0, 'exchange_rate': 7.2},
    {'date': '2024-03-01', 'cny': 20000.0, 'usd': 2778.0, 'exchange_rate': 7.2},
]

def init_sample_data():
    """初始化示例数据到数据库"""
    try:
        # 连接数据库
        with sqlite3.connect('investment_history.db') as conn:
            cursor = conn.cursor()
            
            # 清空现有数据（可选）
            cursor.execute("DELETE FROM investment_history")
            
            # 插入示例数据（使用user_id=1，假设为admin用户）
            for data in SAMPLE_DATA:
                cursor.execute(
                    "INSERT INTO investment_history (date, cny, usd, exchange_rate, user_id) VALUES (?, ?, ?, ?, ?)",
                    (data['date'], data['cny'], data['usd'], data['exchange_rate'], 1)
                )
                
            conn.commit()
            print(f"已成功插入 {cursor.rowcount} 条示例数据")
    except sqlite3.Error as e:
        print(f"初始化示例数据时发生错误: {e}")
    except Exception as e:
        print(f"发生未知错误: {e}")

if __name__ == '__main__':
    init_sample_data()